package com.storyroad.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import com.storyroad.responseClasses.StoryView;
import com.storyroad.utils.DBConnection;
import com.storyroad.utils.TableNames;
import com.storyroad.utils.Utilities;

/**
 * Servlet implementation class GetReportedStories
 */
public class GetReportedStories extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetReportedStories() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * Gets the reported stories in the system. A list of {@link com.storyroad.responseClasses.StoryView} instances which
	 * contain information about the reported story fragments is returned to the front-end.
	 * @param request HttpServletRequest object that contains data coming from front-end
	 * @param response HttpServletResponse object that contains data which is prepared after processing the incoming data
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		ArrayList<StoryView> reportedStories;
		String f_requestType = request.getParameter("request_type");
		if (f_requestType == null || !f_requestType.equals("Android")) {
			f_requestType = "Web";
		}
		PreparedStatement stmt = null;
		ResultSet rs = null;
		PrintWriter out = response.getWriter();
		Connection conn = new DBConnection().connect();
		if (conn == null) {
			out.println("Connection error");
		} else {
			try {
				int s_id;
				String storyTitle;
				reportedStories = new ArrayList<StoryView>();
				stmt = conn.prepareStatement("SELECT s_id FROM " + TableNames.REPORT_TABLE + " ORDER BY date DESC");
				rs = stmt.executeQuery();
				while (rs.next()) {
					s_id = rs.getInt("s_id");
					storyTitle = Utilities.getStoryTitle(conn, s_id);
					reportedStories.add(new StoryView(s_id, storyTitle, "", 0));
				}
				JSONObject reportedStoriesJsonObject = new JSONObject();
				try {
					reportedStoriesJsonObject.put("reportedStories", reportedStories);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				out.print(reportedStoriesJsonObject.toString());

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				out.print(e.getMessage());
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

}
